home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0021_SHLSHR.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  84 lines

  1. { INFO ON SHR and SHL }
  2.  
  3. > (5 Shl 2) + 5 which is: (5 x 4) + 5
  4. > So, 10 * 10 would be (10 Shl 3) + (10 Shl 1)
  5.  
  6. This looks good but, can it be done With Variables (So I can use
  7. numbers other than 5 & 5)?
  8.  
  9.  Yes, just keep in mind that each shift leftward Doubles the value...
  10.  
  11.         p SHL 1  =  p * 2
  12.         p SHL 2  =  p * 4
  13.         p SHL 3  =  p * 8
  14.         p SHL 4  =  p * 16
  15.         ...
  16.  
  17.  (likewise, each shift rightward halves the value).
  18.  
  19.  Also keep in mind that the maximum amount you can shift is the
  20.  number of bits in the Variable.  Bytes are 8 bits, Words and
  21.  Integers are 16 bits, and LongInts are 32 bits.  if you shift
  22.  a Variable its full bit size, or more, it will be 0 (zero).
  23.  
  24.  For example: if p is a Byte, then p SHR 8 = 0.
  25.  
  26. {  Use Shr/Shl to multiply/divide, rather than the operators
  27.   How do you (or anybody) do this?   For example, how would I do 5 * 5?
  28. }
  29. {*******************************************************************}
  30.  Program DemoShifts;
  31.  Var     Number, Result  : Word;
  32.  begin
  33.     {   Calculate 5 * 5, without using multiplication ...           }
  34.  
  35.     Number := 5;                    { original value                }
  36.     Result := Number SHL 2;         { now Result = 4 * Number       }
  37.     Result := Result + Number;      { 4*Number + Number = 5*Number  }
  38.  
  39.     WriteLn( '5 * 5 = ', Result );  { because seeing is believing   }
  40.  
  41.  end {DemoShifts}.
  42. {*******************************************************************}
  43.  
  44.  But TP seems sometimes to do the 'shift vs. MUL optimization' itself,
  45.  this being bad if Compiling For a 386/486 CPU.
  46.  A "* 2" would always result in a SHL instruction ( unless Real
  47.  arithmetic was used ;-> ).
  48.  
  49.  Ok, I understand that part.  if x shr 4 = x/4  (and the remainder is
  50.  dropped) then I Really understand it.  Does it? Do I?
  51.  
  52. No.  x shl 0 = x
  53.      x shl 1 = x/(2^1) = x/2
  54.      x shl 2 = x/(2^2) = x/4
  55.      x shl 3 = x/(2^3) = x/8
  56.      x shl 4 = x/(2^4) = x/16
  57.  
  58. Just as:
  59.      x shr 0 = x
  60.      x shr 1 = x*(2^1) = 2x
  61.      x shr 2 = x*(2^2) = 4x
  62.      x shr 3 = x*(2^3) = 8x
  63.      x shr 4 = x*(2^4) = 16x
  64.  
  65. So now you can see how and why the Compiler substitutes a "shr 1" For "* 2".
  66.  
  67.  > PD> So, 10 * 10 would be: (10 shl 3) + 20
  68.  >
  69.  > MC> not quite:
  70.  > MC> (10 Shl 3)+(10 Shl 1)s, I'm back! (3:634/384.6)
  71.  >
  72.  > Why?  wouldn't the second one take an additional instruction (shl)?
  73.  
  74. Well yes, but 8086 instructions weren't created equal.  PerForming two
  75. shifts and the add to combine them will (on a 286 or lesser) less time
  76. overall than doing even one MUL.
  77.  
  78. The 386/486 has streamlined the MUL instruction so that it takes much less
  79. time, and can often Compete With the shift/add approach.  Which to use?
  80. Well, I'd stick With the shift/add approach, since if you're writing one
  81. Program For both XTs and 386s, the XT will be acceptable, and so will the
  82. 386.  Using the MUL; True, 386 perFormance will be better, but your XT
  83. perFormance will suffer quite a bit.
  84.